/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.swagger;

import com.example.swagger.common.util.PDFHelper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-06-07 22:51
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-06-07
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Slf4j
public class PDFToWord {

    private String pdfPath = "/Users/Yi/Capsule/workspace/ishare-project/docs/api-docs/pdf/swagger-api.pdf";
    private String pdfPath2 = "/Users/Yi/Capsule/workspace/ishare-project/docs/api-docs/pdf2/swagger-api.pdf";
    private String wordPath = "/Users/Yi/Capsule/workspace/ishare-project/docs/api-docs/doc/swagger-api.doc";
    private String pngPath = "/Users/Yi/Capsule/workspace/ishare-project/docs/api-docs/doc/temp.png";

    public void transformV3(){
        //将pdf装图片 并且自定义图片得格式大小
        File file = new File(pdfPath);
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            List<String> imagePaths = Lists.newArrayList();
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 240);
                String tempPath = pngPath.replace(".",i+".");
                imagePaths.add(tempPath);
                ImageIO.write(image, "PNG", new File(tempPath));
            }

            List<String> images=new ArrayList<String>();
            for (String imagePath : imagePaths) {
                images.add(imagePath);
            }
           //合成图片转pdf
           PDFHelper.createPDFFromImage(images,pdfPath2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void transformV1(){
        try{
            File source = new File(pdfPath);
            //通过文件名加载文档
            PDDocument doc = PDDocument.load(source);
            //获取文档的页数
            int pageNumber = doc.getNumberOfPages();
            File word = new File(wordPath);
            if(!word.exists()){
                word.createNewFile();
            }
            //文件输出流
            FileOutputStream fos = new FileOutputStream(word);
            Writer writer = new OutputStreamWriter(fos, "UTF-8");
            //剥离器（读取pdf文件）
            PDFTextStripper stripper = new PDFTextStripper();
            //排序
            stripper.setSortByPosition(true);
            //设置要读取的起始页码
            stripper.setStartPage(1);
            //设置要读取的结束页码
            stripper.setEndPage(pageNumber);
            //pdfbox对中文默认是用空格分隔每一个字，通过这个语句消除空格
            stripper.setWordSeparator("");
            stripper.writeText(doc, writer);
            writer.close();
            fos.close();
        } catch (Exception e){
            log.error("pdf transform to word failed!", e);
        }
    }

    public static void main(String[] args) {
        new PDFToWord().transformV3();
    }
}
